package com.talent.web;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.talent.common.Const;
import com.talent.page.Page;
import com.talent.utils.StringEscapeEditor;
import com.talent.vo.Result;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class BaseController {

    protected HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    protected HttpSession getSession() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        /**
         * 自动转换日期类型的字段格式
         */
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));

        /**
         * 防止XSS攻击
         */
        binder.registerCustomEditor(String.class, new StringEscapeEditor(true, false));
    }

    protected void initPageParams(ModelAndView mv, Page<?> page){
        mv.addObject("currentPage", page.getPageNo());
        mv.addObject("totalPage", page.getTotalPage());
        mv.addObject("totalCount", page.getTotalCount());
    }
    
    /**
     * ajax失败
     * @param msg 失败的消息
     * @return {Object}
     */
    public Object renderError(String msg) {
        Result result = new Result();
        result.setMsg(msg);
        return result;
    }

    /**
     * ajax成功
     * @return {Object}
     */
    public Object renderSuccess() {
        Result result = new Result();
        result.setSuccess(true);
        return result;
    }
    
    /**
     * ajax成功
     * @param msg 消息
     * @return {Object}
     */
    public Object renderSuccess(String msg) {
        Result result = new Result();
        result.setSuccess(true);
        result.setMsg(msg);
        return result;
    }

    /**
     * ajax成功
     * @param obj 成功时的对象
     * @return {Object}
     */
    public Object renderSuccess(Object obj) {
        Result result = new Result();
        result.setSuccess(true);
        result.setObj(obj);
        return result;
    }
    /**
     * ajax成功
     * @param obj 成功时的对象
     * @param msg 消息
     * @return {Object}
     */
    public Object renderSuccess(String msg, Object obj) {
    	Result result = new Result();
    	result.setSuccess(true);
    	result.setObj(obj);
    	result.setMsg(msg);
    	return result;
    }
    
    /**
     * ajax成功
     * @param obj 成功时的对象
     * @param msg 消息
     * @return {Object}
     */
    public Object renderSuccess(String msg, Object obj,Object obj1) {
    	Result result = new Result();
    	result.setSuccess(true);
    	result.setObj(obj);
    	result.setObj(obj1);
    	result.setMsg(msg);
    	return result;
    }
    
    @SuppressWarnings("unchecked")
	public  <User> User getCurrentUser(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return user;
    }
}
